package com.多线程07.demo015;

public class SellTicket extends Thread {

    //准备票
    private static int ticketCount = 100;

    //private static Object obj = new Object();//当成是锁

    public SellTicket() {
    }


    public SellTicket(String name) {
        super(name);
    }

    public synchronized void run() {
        while (true){
            //同一时间只能有一个对象获得锁

            if (ticketCount<=0){
                break;
            }

            System.out.println(Thread.currentThread().getName()+"当前正在卖第"+ticketCount+"张票");

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            ticketCount--;
        }

        System.out.println("票卖完了");
    }


    public static void main(String[] args) {

        SellTicket t1 = new SellTicket("窗口1");
        SellTicket t2 = new SellTicket("窗口2");
        SellTicket t3 = new SellTicket("窗口3");

        t1.start();
        t2.start();
        t3.start();

    }


}
